package com.atguigu.gmall;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 异步任务 串行，并行
 * @author: atguigu
 * @create: 2023-01-03 15:36
 */
public class CompletableFutureDemo2 {

    /**
     * A 线程有计算结果
     * B 线程依赖A线程计算结果，执行B线程任务没有返回值
     * C 线程依赖A线程计算结果，执行C线程任务没有返回值
     * @param args
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
       //1.创建异步任务对象 CompletableFuture  A任务需要返回值
        CompletableFuture<Long> futureA = CompletableFuture.supplyAsync(() -> {
            System.out.println("A任务执行");
            return 29L;
        });

        //2.基于上面对象 构建 B任务对象
        CompletableFuture<Void> futureB = futureA.thenAcceptAsync((aResult) -> {
            System.out.println("B任务得到A任务执行结果：" + aResult + ",执行B任务无需返回结果");
        });


        //3.基于上面对象 构建 C任务对象
        CompletableFuture<Void> futureC = futureA.thenAcceptAsync((aResult) -> {
            System.out.println("C任务执行:"+aResult+",执行C任务无需返回结果");
        });

        futureA.join();
        futureB.join();
        futureC.join();

        //Long a = futureA.get();
        //System.out.println(a);
        //String b = futureB.get();
        //System.out.println(b);
        //String c = futureC.get();
        //System.out.println(c);
    }
}
